# controlls the creation deletion and manipulation of teams associated with projects
class TeamsController < ApplicationController
  include EnotifyLdap
  
  layout 'admin_standard'
  before_filter :authenticate
  
  
  active_scaffold :team
  
  active_scaffold :team do |config|
       config.label = '' 
       config.show.link.page = true
       config.columns = [:manager, :count, :projections]
       config.list.per_page = 100
       config.list.sorting = [{ :person => :desc}]
       config.columns[:person].label = 'Manager'
       config.columns[:count].label = 'Team Members'
       
       config.actions.exclude :update, :create, :show
       
  end


  ##
  # show the teams for a project
  def for_project
    @project = Project.find(params[:id])
    
    respond_to do |format|
      format.html 
      format.xml  { render :xml => @teams }
    end
  end


  ##
  # show a team
  def show
    @team = Team.find(params[:id])
    @project = @team.project
   
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @team }
    end
  end


  ##
  #  display the team creation page
  def new
    @team = Team.new
    @project = Project.find(params[:id])
    

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @team }
    end
  end
  
  # GET /teams/1/edit
  def edit
    @team = Team.find(params[:id])
  end

  # POST /teams
  # POST /teams.xml
  def create
    @username = params[:team][:username]
    @project = Project.find(params[:team][:project_id])
    @manager = get_person_from_ldap_by_username(params[:team][:username])
    if !@manager.nil?
      @manager.save
      
      
      # create and populate the team
      @team = Team.new()
      @team.project = @project
      @team.person = @manager
    
      #make the manager a member of the team
      @owner = Owner.new
      @owner.team = @team
      @owner.person = @manager

      
      if params[:team][:use_directory]
        # Lookup the team in LDAP
        team_members = get_array_of_employees_from_ldap_by_manager_username(@manager.username)
        
        team_members.each do |team_member|
          # save the team member
          team_member.save
          
          # Add this person to the team 
          owner = Owner.new
          owner.team = @team
          owner.person = team_member
          owner.save
        end
        
      end
        
        

      respond_to do |format|
        if @owner.save && @team.save
          flash[:notice] = 'Team was successfully created. username:' + params[:team][:username].to_s 
        
          format.html { redirect_to(:controller => '/teams', :action => :show, :id => @team.id) }
          format.xml  { render :xml => @team, :status => :created, :location => @team }
        else
          flash[:notice] = 'Something went wrong.'
        
          format.html { render :action => "new" }
          format.xml  { render :xml => @team.errors, :status => :unprocessable_entity }
        end
      end
    else  ## if manager is nill
      respond_to do |format|
          flash[:notice] = 'The username you provided does not belong to one of our employees. Do not freak out! This message is NOT endorsed by the Human Resources Department.'
         
          format.html { render :action => "new" }
          format.xml  { render :xml => @team.errors, :status => :unprocessable_entity }
      end
      
      
    end
  end


  # DELETE /teams/1
  # DELETE /teams/1.xml
  def destroy
    @team = Team.find(params[:id])
    @team.destroy

    respond_to do |format|
      format.html { redirect_to(teams_url) }
      format.xml  { head :ok }
    end
  end

end
